<?php

use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\ProcessIdProcessor;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Application;

return [
    'config.server' => DI\value(config('server')),
    // 数据库连接
    PDO::class => DI\factory(function () {
        return new PDO(
            sprintf('mysql:host=%s;port=%d;dbname=%s',
                config('database.connections.mysql.host', '127.0.0.1'),
                config('database.connections.mysql.port', 3306),
                config('database.connections.mysql.database', 'swige')
            ),
            config('database.connections.mysql.username', 'swige_user'),
            config('database.connections.mysql.password', 'swige_pass'),
            [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
            ]
        );
    }),

    // Redis连接
    \Redis::class => DI\factory(function () {
        $redis = new \Redis();
        $redis->connect(config('cache.stores.redis.connection.host','127.0.0.1'), config('cache.stores.redis.connection.port', 6379));
        if (!empty(config('cache.stores.redis.connection.password'))) {
            $redis->auth(config('cache.stores.redis.connection.password'));
        }
        return $redis;
    }),

    Application::class => \DI\factory(function (ContainerInterface $c) {
        return new Application(config('app.name', 'Swige'), config('app.version', '1.0.0'));
    }),

    LoggerInterface::class => \DI\factory(function (ContainerInterface $c) {
        $channel = config('log.default_channel');
        $channelConfig = config('log.channels')[$channel];
        $logger = new Logger($channel);
        // 主日志处理器
        $logger->pushHandler(new RotatingFileHandler(
            $channelConfig['path'],
            $channelConfig['days'],
            Logger::toMonologLevel($channelConfig['level']),
            true,
            $channelConfig['permission'] ?? 0664
        ));

        // 调试模式下添加控制台输出
        if (config('app.debug')) {
            $logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
        }
        // 添加处理器
        $logger->pushProcessor(new PsrLogMessageProcessor());
        $logger->pushProcessor(new MemoryUsageProcessor());
        $logger->pushProcessor(new ProcessIdProcessor());

        return $logger;
    }),
    'request_logger' => \DI\factory(function () {
        $config = config('log.channels.request');

        $logger = new Logger('request');
        $logger->pushHandler(new RotatingFileHandler(
            config('log.channels.database.path'),
            config('log.channels.database.days'),
            Logger::toMonologLevel(config('log.channels.database.level'))
        ));
        return $logger;
    }),
    'database_logger' => \DI\factory(function () {
        $logger = new Logger('database');
        $logger->pushHandler(new RotatingFileHandler(
            config('log.channels.database.path'),
            config('log.channels.database.days'),
            Logger::toMonologLevel(config('log.channels.database.level'))
        ));
        return $logger;
    }),
    \Swoole\Database\RedisPool::class => DI\factory(function () {
        return new \Swoole\Database\RedisPool((new \Swoole\Database\RedisConfig)
                ->withHost(config('cache.stores.redis.connection.host', '127.0.0.1'))
                ->withPort(config('cache.stores.redis.connection.port', 6379))
                ->withAuth(config('cache.stores.redis.connection.password', '')),
            16
        );
    }),
    //  ->withAuth('foobared')
    \Swoole\Database\PDOPool::class => DI\factory(function () {
        return new \Swoole\Database\PDOPool((new \Swoole\Database\PDOConfig)
            ->withHost(config('database.connections.mysql.host', '127.0.0.1'))
            ->withPort(config('database.connections.mysql.port', 3306))
            ->withDbName(config('database.connections.mysql.database', 'swige'))
            ->withCharset('utf8mb4')
            ->withUsername(config('database.connections.mysql.username', 'username'))
            ->withPassword(config('database.connections.mysql.password', 'password')), 16);
    }),
    \Swoole\Coroutine\Channel::class => DI\factory(function () {
        return new \Swoole\Coroutine\Channel(200);
    }),
    \Swoole\Thread\Map::class => DI\factory(function () {
        return new \Swoole\Thread\Map();
    }),
];
